package com.example.mybleapp;

import androidx.appcompat.app.AppCompatActivity;

import com.github.mikephil.charting.charts.LineChart;

import android.app.AlertDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Button;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class ChartActivity extends AppCompatActivity{

    private DynamicLineChartManager dynamicLineChartManager1;
    private DynamicLineChartManager dynamicLineChartManager2;
    private List<Integer> list1 = new ArrayList<>(); //数据集合
    private List<Integer> list2 = new ArrayList<>(); //数据集合
    private List<String> names = new ArrayList<>(); //折线名字集合
    private List<Integer> colour = new ArrayList<>();//折线颜色集合
    private MyInterface myInterface = new MyInterface();
    private FileOperation mFileOperation = new FileOperation();
    private Button start_stop;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chart);
        LineChart mChart1 = findViewById(R.id.dynamic_chart1);
        LineChart mChart2 = findViewById(R.id.dynamic_chart2);
        start_stop = findViewById(R.id.start_stop);
        if(myInterface.getCollect()){
            start_stop.setText("停止采集");
        } else {
            start_stop.setText("开始采集");
        }
        registerReceiver(mGattUpdateReceiver, makeGattUpdateIntentFilter());
        //折线名字
        names.add("Line1");
        names.add("Line2");
        names.add("Line3");
        //折线颜色
        colour.add(Color.CYAN);
        colour.add(Color.GREEN);
        colour.add(Color.BLUE);

        dynamicLineChartManager1 = new DynamicLineChartManager(mChart1, names, colour);
        dynamicLineChartManager2 = new DynamicLineChartManager(mChart2, names, colour);
        dynamicLineChartManager1.setYAxis(20, -20, 20);
        dynamicLineChartManager2.setYAxis(2000, -2000, 20);
    }

    private final BroadcastReceiver mGattUpdateReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            final String action = intent.getAction();
            if (BluetoothLeService.ACTION_DATA_AVAILABLE.equals(action)) {
                int[] ReceiveData = intent.getExtras().getIntArray(BluetoothLeService.EXTRA_DATA);
                if (ReceiveData != null && myInterface.getCollect()) {
                    if(ReceiveData.length != 6){
                        return;
                    }
                    list1.clear();
                    list2.clear();
                    for (int i=0 ; i<3;i++) {
                        //Log.i("ChartActivity", String.valueOf(ReceiveData[i]));
                        list1.add(ReceiveData[i]/1638);
                    }
                    for(int i=0;i<3;i++){
                        //Log.i("ChartActivity", ReceiveData[i+3]);
                        list2.add(ReceiveData[i + 3]);
                    }
//                    Log.i("ChartActivity", String.valueOf(list.size()));
                    dynamicLineChartManager1.addEntry(list1);
                    dynamicLineChartManager2.addEntry(list2);
                }
            }
        }
    };

    private static IntentFilter makeGattUpdateIntentFilter() {
        final IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(BluetoothLeService.ACTION_DATA_AVAILABLE);
        return intentFilter;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(mGattUpdateReceiver);
    }

    private void CreateDialog(String message){
        final AlertDialog.Builder builder = new AlertDialog.Builder(ChartActivity.this);
        builder.setTitle("文件保存路径为：").setMessage(message).setPositiveButton("好的", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
            }
        }).setCancelable(false).show();
    }

    public void onStart(View view) {
        if(myInterface.getCollect()){
            myInterface.setCollect(false);
            start_stop.setText("开始采集");
            String file = mFileOperation.CloseFile();
            if(file != null){
                CreateDialog(file);
            }
        }else{
            myInterface.setCollect(true);
            start_stop.setText("停止采集");
            File file = getExternalFilesDir(Environment.getExternalStorageState());
            mFileOperation.OpenFile(file);
        }
    }
}
